package com.github.shiqiyue.app.common.trace;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.MDC;

/***
 * 定时任务切面
 * @author wenyao.wu
 * @date 2019/11/1
 */
@Slf4j
@Aspect
public class ScheduleTraceIdAspect {

    @Around(value = "@annotation(org.springframework.scheduling.annotation.Scheduled)")
    public void doAround(ProceedingJoinPoint joinPoint) {
        MDC.put(TraceConstants.TRACE_ID, IdWorker.getIdStr());
        try {
            joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        MDC.remove(TraceConstants.TRACE_ID);
    }

}
